﻿using Dev.DevNet.Common.Utility;
using Dev.DevNet.IServices.DevSystem;
using Dev.DevNet.Services.DevSystem;
using log4net;
using log4net.Config;
using SqlSugar;

namespace Dev.DevNet.WebAPI.Utility.DI
{
    /// <summary>
    /// 程序依赖注入
    /// </summary>
    public static class ServiceDIUtility
    {
        public static void AddDevService(this WebApplicationBuilder builder)
        {
            builder.Services.AddTransient<ISqlSugarClient>(option =>
            {
            //var constr = builder.Configuration["ConnectionStrings:MysqlConn"];
            //    Console.WriteLine($"配置文件：{constr}");
            SqlSugarClient client = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = builder.Configuration["ConnectionStrings:MysqlConn"],
                    DbType = DbType.MySql,
                    InitKeyType = InitKeyType.Attribute,
                    IsAutoCloseConnection = true,
                //    SlaveConnectionConfigs = new List<SlaveConnectionConfig>() {//主从复制需要
                //     new SlaveConnectionConfig() { HitRate=10, ConnectionString="Data Source=DESKTOP-T2D6ILD;Initial Catalog=SqlSugarCustomerDB_001;Persist Security Info=True;User ID=sa;Password=sa123" } ,
                //     new SlaveConnectionConfig() { HitRate=10, ConnectionString="Data Source=DESKTOP-T2D6ILD;Initial Catalog=SqlSugarCustomerDB_002;Persist Security Info=True;User ID=sa;Password=sa123" },
                //     new SlaveConnectionConfig() { HitRate=10, ConnectionString="Data Source=DESKTOP-T2D6ILD;Initial Catalog=SqlSugarCustomerDB_003;Persist Security Info=True;User ID=sa;Password=sa123" },

                //}
                });

                client.Aop.OnLogExecuting = (sql, par) =>
                {
                    Console.WriteLine($"--------------------------------SQL-------------------------------------------------------------");
                    Console.WriteLine($"Sql语句：{sql}");
                };

                return client;

            });

            //自己业务实体类
            builder.Services.AddWooService();
           


        }
        /// <summary>
        /// 添加自己业务服务类注册
        /// 完成依赖注入
        /// </summary>
        /// <param name="services">服务集合对象</param>
        public static void AddWooService(this IServiceCollection services)
        {
            //日志
            Log4netHelper.Repository = LogManager.CreateRepository("DevLog4Repository");
            XmlConfigurator.Configure(Log4netHelper.Repository, new FileInfo(Environment.CurrentDirectory + "/Config/log4net.config"));

            services.AddTransient<IDevUserInfoService, DevUserInfoService>();
            
        }


    }
}
